const jwt = require("jsonwebtoken")
//撒盐，加密时候混淆
const secret = '123bingjspoiuytrewqlkjhgfdsa'
const {status} = require('./pro_mysql');
//生成token
//info也就是payload是需要存入token的信息
function createToken({loginPhone,loginPassword,ID}) {
    let token = jwt.sign({
        loginPhone,
        loginPassword,
        ID,
        data: Date.now()
    }, secret, {
        //Token有效时间 单位s
        expiresIn:60 * 60 * 24 *7
    })
    return token
}

//验证Token
async function verifyToken(token) {
    return await new Promise((resolve, reject) => {
        jwt.verify(token, secret, (error, result) => {
            if(error){
                resolve(status(0,error))
            } else {
                resolve(status(1,result))
            }
        })
    })
}

module.exports = {
    createToken,
    verifyToken
}